<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%
    // 绑定就诊卡
    PatientCard patientCard = PatientCard.fromMap(parameters, PatientCard.class);
    patientCard.setUid(token.getUid());
    Integer pid = patientCard.getPid();
    Integer branch = patientCard.getBranch();
    if (isEmpty(branch)) {
        patientCard.setBranch(hospital);
    }
    List<String> branchName = QBuilder.db("basedb").select("name").from("Hospital").where(new Where("hospital=", patientCard.getBranch()).and("status=", STATUS_ENABLE)).query().getResultObj();
    if (isEmpty(branchName)) {
        Tools.encryptPrint(out, new Message(30000, "就诊卡添加失败"));
        return;
    }
    patientCard.setBranchName(branchName.get(0));
    String cardNumber = patientCard.getNumber().trim();
    if (cardNumber.length() > 30) {
        Tools.encryptPrint(out, new Message(30000, "就诊卡号不能大于30位"));
        return;
    }
    if (cardNumber.length() == 0) {
        Tools.encryptPrint(out, new Message(30000, "就诊卡号不能为空"));
        return;
    }
    List<UserProfile> userProfiles = QBuilder.db("basedb").select().from("UserProfile").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and("id=", pid).and("uid=", token.getUid())).query().getResultObj();
    if (isEmpty(userProfiles)) {
        Tools.encryptPrint(out, new Message(30041, "亲属信息不存在"));
        return ;
    }
    Where where = new Where();
    if (!isEmpty(patientCard.getCardName())) {
        where = new Where("cardName=", patientCard.getCardName());
    }
    List<PatientCard> patientCards = QBuilder.db("basedb").select().from("PatientCard").where(where.and("hospital=", hospital).and("branch=", patientCard.getBranch()).and("status = ", STATUS_ENABLE).and("number=", cardNumber)).query().getResultObj();
    if (!isEmpty(patientCards)) {
        Tools.encryptPrint(out, new Message(30042, patientCard.getCardName() + ":" + cardNumber + "已经被绑定"));
        return ;
    }
    if ("市民卡".equals(patientCard.getCardName())) {
        patientCards = QBuilder.db("basedb").select().from("PatientCard").where(new Where("cardName=", patientCard.getCardName()).and("hospital=", hospital).and("status = ", STATUS_ENABLE).and("number=", cardNumber)).query().getResultObj();
        if (!isEmpty(patientCards)) {
            Tools.encryptPrint(out, new Message(30042, patientCard.getCardName() + ":" + cardNumber + "已经被绑定"));
            return ;
        }
        patientCards = QBuilder.db("basedb").select().from("PatientCard").where(new Where("cardName=", patientCard.getCardName()).and("hospital=", hospital).and("status = ", STATUS_ENABLE).and("pid=", pid)).query().getResultObj();
        if (!isEmpty(patientCards)) {
            Tools.encryptPrint(out, new Message(30042, "您已经绑定过市民卡了"));
            return ;
        }
    }
    Integer id = (Integer)DBTools.db("basedb").addItem(patientCard);
    if (isEmpty(id)) {
        Tools.encryptPrint(out, new Message(30000, "就诊卡添加失败"));
        return;
    }
    List<Map> cards = card(patientCard);
    if (isEmpty(cards)) {
        msg = new Message(30041, "暂未添加就诊卡");
    }
    // add by liuzq for 更新病人卡号
    if(hospital == 1113) {
    	
    	/**
    	**更新病人卡号 String pid(select * from userprofile where uid = '112077'取身份证号), String cardNumber（卡号）, String card_name
    	**/
        String cardNo = cardNumber;// 新卡号
        String idCard = userProfiles.get(0).getCardNumber(); // 身份证号
        Map<String, String> map2 = new HashMap<String, String>();
        map2.put("cardNo", cardNo);//身份证号
        map2.put("idCard", idCard);
        String url = HOST_BASE_URL_1113 + "/appoint/updatepatientcardno.jsp";
        String json = HttpUtil.post(url,map2);
       	msg =  JSONUtil.fromJson(json, new TypeReference<Message>(){});
    }
    msg.put("data", cards);
    Tools.encryptPrint(out, msg);
%>
